
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>字典 · relax space</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="relax space <xiaoxm_001@outlook.com>">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-disqus/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../list/m1.html" />
    
    
    <link rel="prev" href="../param/m1.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    relax-space
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../../tech/">
            
                <a href="../../tech/">
            
                    
                    技术
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../../tech/mysql/m1.html">
            
                <a href="../../tech/mysql/m1.html">
            
                    
                    mysql
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../../tech/rocketmq/m1.html">
            
                <a href="../../tech/rocketmq/m1.html">
            
                    
                    rocketmq
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../../tool/">
            
                <a href="../../tool/">
            
                    
                    工具
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../../tool/git/m1.html">
            
                <a href="../../tool/git/m1.html">
            
                    
                    git
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1.1" data-path="../../tool/git/rebase/m1.html">
            
                <a href="../../tool/git/rebase/m1.html">
            
                    
                    rebase
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../../tool/idea/m1.html">
            
                <a href="../../tool/idea/m1.html">
            
                    
                    idea
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.2.1" data-path="../../tool/idea/jrebel/m1.html">
            
                <a href="../../tool/idea/jrebel/m1.html">
            
                    
                    jrebel
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.2" data-path="../../tool/idea/git/m1.html">
            
                <a href="../../tool/idea/git/m1.html">
            
                    
                    git本地改变
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../../tool/navicat/m1.html">
            
                <a href="../../tool/navicat/m1.html">
            
                    
                    navicat
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../../tool/system/m1.html">
            
                <a href="../../tool/system/m1.html">
            
                    
                    system
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.4.1" data-path="../../tool/system/proxy/m1.html">
            
                <a href="../../tool/system/proxy/m1.html">
            
                    
                    代理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4.2" data-path="../../tool/system/ssh/m1.html">
            
                <a href="../../tool/system/ssh/m1.html">
            
                    
                    ssh
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4.3" data-path="../../tool/system/hotkey.html">
            
                <a href="../../tool/system/hotkey.html">
            
                    
                    快捷键
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../../tool/vscode/m1.html">
            
                <a href="../../tool/vscode/m1.html">
            
                    
                    vscode
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.5.1" data-path="../../tool/vscode/case/m1.html">
            
                <a href="../../tool/vscode/case/m1.html">
            
                    
                    大小写快捷键
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.2" data-path="../../tool/vscode/proxy/m1.html">
            
                <a href="../../tool/vscode/proxy/m1.html">
            
                    
                    代理
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../../tool/windows/m1.html">
            
                <a href="../../tool/windows/m1.html">
            
                    
                    windows
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.6.1" data-path="../../tool/windows/ipSwitch/m1.html">
            
                <a href="../../tool/windows/ipSwitch/m1.html">
            
                    
                    ip切换工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6.2" data-path="../../tool/windows/pin/m1.html">
            
                <a href="../../tool/windows/pin/m1.html">
            
                    
                    pin
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6.3" data-path="../../tool/windows/time/m1.html">
            
                <a href="../../tool/windows/time/m1.html">
            
                    
                    自动同步时间
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../../docker/">
            
                <a href="../../docker/">
            
                    
                    docker
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../../docker/it/m1.html">
            
                <a href="../../docker/it/m1.html">
            
                    
                    it命令
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../../java/">
            
                <a href="../../java/">
            
                    
                    java
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../../java/maven/m1.html">
            
                <a href="../../java/maven/m1.html">
            
                    
                    maven
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../../java/fastjson/m1.html">
            
                <a href="../../java/fastjson/m1.html">
            
                    
                    序列化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../../java/base1/m1.html">
            
                <a href="../../java/base1/m1.html">
            
                    
                    基础1
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../">
            
                <a href="../">
            
                    
                    python
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../source/m1.html">
            
                <a href="../source/m1.html">
            
                    
                    清华源
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../zone/m1.html">
            
                <a href="../zone/m1.html">
            
                    
                    时区
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../email/m1.html">
            
                <a href="../email/m1.html">
            
                    
                    邮件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../str/m1.html">
            
                <a href="../str/m1.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../param/m1.html">
            
                <a href="../param/m1.html">
            
                    
                    参数
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.6.6" data-path="m1.html">
            
                <a href="m1.html">
            
                    
                    字典
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../list/m1.html">
            
                <a href="../list/m1.html">
            
                    
                    列表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../set/m1.html">
            
                <a href="../set/m1.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../re/m1.html">
            
                <a href="../re/m1.html">
            
                    
                    正则
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../serialize/m1.html">
            
                <a href="../serialize/m1.html">
            
                    
                    序列化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.11" data-path="../pypy/m1.html">
            
                <a href="../pypy/m1.html">
            
                    
                    pypy安装
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../../golang/">
            
                <a href="../../golang/">
            
                    
                    golang
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../../golang/hello/m1.html">
            
                <a href="../../golang/hello/m1.html">
            
                    
                    hello
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../../golang/speed/m1.html">
            
                <a href="../../golang/speed/m1.html">
            
                    
                    加速
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../../golang/hello-mod/m1.html">
            
                <a href="../../golang/hello-mod/m1.html">
            
                    
                    hello-mod
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../../vip/">
            
                <a href="../../vip/">
            
                    
                    vip
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >字典</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x5B57;&#x5178;">&#x5B57;&#x5178;</h1>
<ol>
<li>&#x589E;&#x5220;&#x6539;&#x67E5;</li>
<li>in, enumerate</li>
<li>setdefault,update,get,pop,popitem</li>
<li>sort</li>
<li>&#x6D45;&#x590D;&#x5236; &#x548C; &#x6DF1;&#x5EA6;&#x590D;&#x5236;</li>
<li>&#x65B9;&#x6CD5;&#xFF1A;dict(p)</li>
</ol>
<pre><code class="lang-python">
<span class="hljs-keyword">import</span> copy


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_1</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-comment"># &#x589E;&#x5220;&#x6539;&#x67E5;</span>
    dict1 = {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>}
    dict1[<span class="hljs-string">&apos;c&apos;</span>] = <span class="hljs-number">3</span>
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>, <span class="hljs-string">&apos;c&apos;</span>: <span class="hljs-number">3</span>} == dict1, <span class="hljs-string">&apos;dict &#x65B0;&#x589E;&#x5931;&#x8D25;&apos;</span>

    <span class="hljs-keyword">del</span> dict1[<span class="hljs-string">&apos;b&apos;</span>]
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;c&apos;</span>: <span class="hljs-number">3</span>} == dict1, <span class="hljs-string">&apos;dict &#x5220;&#x9664;&#x5931;&#x8D25;&apos;</span>
    dict1[<span class="hljs-string">&apos;c&apos;</span>] = <span class="hljs-number">4</span>
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;c&apos;</span>: <span class="hljs-number">4</span>} == dict1, <span class="hljs-string">&apos;dict &#x4FEE;&#x6539;&#x5931;&#x8D25;&apos;</span>

    c = dict1[<span class="hljs-string">&apos;c&apos;</span>]
    <span class="hljs-keyword">assert</span> <span class="hljs-number">4</span> == c, <span class="hljs-string">&apos;dict &#x67E5;&#x8BE2;&#x5931;&#x8D25;&apos;</span>
    <span class="hljs-keyword">pass</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-comment"># in, enumerate</span>
    dict1 = {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>}
    indexs = [i <span class="hljs-keyword">for</span> i, v <span class="hljs-keyword">in</span> enumerate(dict1.items()) <span class="hljs-keyword">if</span> v[<span class="hljs-number">0</span>] == <span class="hljs-string">&apos;b&apos;</span>]
    <span class="hljs-keyword">assert</span> [<span class="hljs-number">1</span>] == indexs, <span class="hljs-string">&apos;dict enumerate error&apos;</span>

    <span class="hljs-keyword">assert</span> <span class="hljs-string">&apos;a&apos;</span> <span class="hljs-keyword">in</span> dict1, <span class="hljs-string">&apos;dict in error&apos;</span>
    <span class="hljs-keyword">pass</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_3</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-comment"># setdefault,update,get,pop,popitem</span>
    <span class="hljs-comment"># setdefault &#x5982;&#x679C;&#x6709;&#x6CA1;&#x5219;&#x589E;&#x52A0;&#xFF0C;&#x6709;&#x5219;&#x4E0D;&#x53D8;</span>
    dict1 = {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>}
    dict1.setdefault(<span class="hljs-string">&apos;c&apos;</span>, <span class="hljs-number">4</span>)
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>, <span class="hljs-string">&apos;c&apos;</span>: <span class="hljs-number">4</span>}, <span class="hljs-string">&apos;dict setdefault error&apos;</span>

    dict1.setdefault(<span class="hljs-string">&apos;b&apos;</span>, <span class="hljs-number">1</span>)
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>, <span class="hljs-string">&apos;c&apos;</span>: <span class="hljs-number">4</span>} == dict1, <span class="hljs-string">&apos;dict setdefault b error&apos;</span>

    dict1.update({<span class="hljs-string">&apos;c&apos;</span>: <span class="hljs-number">5</span>})
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>, <span class="hljs-string">&apos;c&apos;</span>: <span class="hljs-number">5</span>} == dict1, <span class="hljs-string">&apos;dict update error&apos;</span>

    c = dict1.get(<span class="hljs-string">&apos;c&apos;</span>, <span class="hljs-number">1</span>)
    <span class="hljs-keyword">assert</span> <span class="hljs-number">5</span> == c, <span class="hljs-string">&apos;dict get error&apos;</span>

    f = dict1.get(<span class="hljs-string">&apos;f&apos;</span>, <span class="hljs-number">1</span>)
    <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span> == f, <span class="hljs-string">&apos;dict get f error&apos;</span>

    dict1.pop(<span class="hljs-string">&apos;c&apos;</span>)
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>} == dict1, <span class="hljs-string">&apos;dict pop error&apos;</span>

    dict1.popitem()
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>} == dict1, <span class="hljs-string">&apos;dict popitem error&apos;</span>

    <span class="hljs-keyword">pass</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_4</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-comment"># sort</span>
    dict1 = {<span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>, <span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>}
    dict2 = dict(sorted(dict1.items(), key=<span class="hljs-keyword">lambda</span> kv: kv[<span class="hljs-number">0</span>]))
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>} == dict2, <span class="hljs-string">&apos;dict sorted 1 error&apos;</span>

    dict1 = {<span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>, <span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;c&apos;</span>: <span class="hljs-number">1</span>}
    dict2 = dict(sorted(dict1.items(), key=<span class="hljs-keyword">lambda</span> kv: (kv[<span class="hljs-number">1</span>], kv[<span class="hljs-number">0</span>])))
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;c&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>}

    <span class="hljs-keyword">pass</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_5</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-comment"># &#x6D45;&#x590D;&#x5236; &#x548C; &#x6DF1;&#x5EA6;&#x590D;&#x5236;</span>
    <span class="hljs-comment"># 1.&#x6D45;&#x590D;&#x5236;&#xFF1A;&#x4E0D;&#x6539;&#x53D8;&#x539F;&#x5B57;&#x5178;</span>
    dict1 = {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>}
    dict2 = dict1.copy()
    dict2.update({<span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">1</span>})
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>} == dict1, <span class="hljs-string">&apos;dict copy 1 error&apos;</span>
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">1</span>} == dict2, <span class="hljs-string">&apos;dict copy 1.1 error&apos;</span>

    <span class="hljs-comment"># 2.&#x6D45;&#x590D;&#x5236;&#xFF1A;&#x6539;&#x53D8;&#x539F;&#x5B57;&#x5178;--&#x5B50;&#x5BF9;&#x8C61;</span>
    dict1 = {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: {<span class="hljs-string">&apos;c&apos;</span>: <span class="hljs-number">1</span>}}
    dict2 = dict1.copy()
    dict2[<span class="hljs-string">&apos;b&apos;</span>].setdefault(<span class="hljs-string">&apos;d&apos;</span>, <span class="hljs-number">2</span>)
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: {<span class="hljs-string">&apos;c&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;d&apos;</span>: <span class="hljs-number">2</span>}} == dict1, <span class="hljs-string">&apos;dict copy 2 error&apos;</span>
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: {<span class="hljs-string">&apos;c&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;d&apos;</span>: <span class="hljs-number">2</span>}} == dict2, <span class="hljs-string">&apos;dict copy 2.1 error&apos;</span>

    <span class="hljs-comment"># 3.&#x6D45;&#x590D;&#x5236;&#xFF1A;&#x4E0D;&#x6539;&#x53D8;&#x539F;&#x5B57;&#x5178;</span>
    dict1 = {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: {<span class="hljs-string">&apos;c&apos;</span>: <span class="hljs-number">1</span>}}
    dict2 = copy.deepcopy(dict1)
    dict2[<span class="hljs-string">&apos;b&apos;</span>].setdefault(<span class="hljs-string">&apos;d&apos;</span>, <span class="hljs-number">2</span>)
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: {<span class="hljs-string">&apos;c&apos;</span>: <span class="hljs-number">1</span>}} == dict1, <span class="hljs-string">&apos;dict copy 3 error&apos;</span>
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: {<span class="hljs-string">&apos;c&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;d&apos;</span>: <span class="hljs-number">2</span>}} == dict2, <span class="hljs-string">&apos;dict copy 3.1 error&apos;</span>

    <span class="hljs-keyword">pass</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_6</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-comment"># &#x65B9;&#x6CD5;&#xFF1A;dict(p)</span>
    dict1 = dict()
    <span class="hljs-keyword">assert</span> {} == dict1, <span class="hljs-string">&apos;dict new 1 error&apos;</span>

    dict1 = dict({<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>})
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>} == dict1, <span class="hljs-string">&apos;dict new 2 error&apos;</span>

    dict1 = dict([(<span class="hljs-string">&apos;a&apos;</span>, <span class="hljs-number">1</span>), (<span class="hljs-string">&apos;b&apos;</span>, <span class="hljs-number">2</span>)])
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>} == dict1, <span class="hljs-string">&apos;dict new 3 error&apos;</span>

    dict1 = dict(a=<span class="hljs-number">1</span>, b=<span class="hljs-number">2</span>)
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>} == dict1, <span class="hljs-string">&apos;dict new 4 error&apos;</span>

    params = {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>}
    dict1 = dict(**params)
    <span class="hljs-keyword">assert</span> {<span class="hljs-string">&apos;a&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;b&apos;</span>: <span class="hljs-number">2</span>} == dict1, <span class="hljs-string">&apos;dict new 5 error&apos;</span>
    <span class="hljs-keyword">pass</span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../param/m1.html" class="navigation navigation-prev " aria-label="Previous page: 参数">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../list/m1.html" class="navigation navigation-next " aria-label="Next page: 列表">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"字典","level":"1.6.6","depth":2,"next":{"title":"列表","level":"1.6.7","depth":2,"path":"python/list/m1.md","ref":"python/list/m1.md","articles":[]},"previous":{"title":"参数","level":"1.6.5","depth":2,"path":"python/param/m1.md","ref":"python/param/m1.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-search","disqus","toggle-chapters","chapter-fold","search-pro","code","splitter","github","donate"],"ignores":["_book","node_modules"],"root":".","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"chapter-fold":{},"disqus":{"useIdentifier":false,"shortName":"relax-space"},"github":{"url":"https://github.com/relax-space"},"splitter":{},"search-pro":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"code":{"copyButtons":true},"donate":{"alipay":"https://relax.elandcloud.com/resource/barcode/a1.jpg","alipayText":"支付宝打赏","button":"赏","title":"","wechat":"https://relax.elandcloud.com/resource/barcode/w1.png","wechatText":"微信打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"toggle-chapters":{}},"theme":"default","author":"relax space <xiaoxm_001@outlook.com>","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"relax space","language":"zh-hans","gitbook":"3.2.3","description":"relax space"},"file":{"path":"python/dict/m1.md","mtime":"2022-09-20T17:14:05.987Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-09-20T17:15:01.763Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/URI.js/1.16.1/URI.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-disqus/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

